package com.woniu.controller;

import com.woniu.entity.Vehicle;
import com.woniu.service.VehicleService;
import com.woniu.util.ResponseData;
import com.woniu.util.ResponseEnum;
import com.woniu.vo.VehicleVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * FileName: VehicleController
 * Author: wujiajun
 * Date:   2025/2/17 14:51
 * Description:DOTO
 */
@RestController
@RequestMapping("/vehicle")
public class VehicleController {
    @Resource
    private VehicleService vehicleService;

    /**
     *汽车列表查询
     */
    @GetMapping("/list")
    public ResponseData<?> getVehicleList(
            @RequestParam(required = false) String keyword,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        List<VehicleVO> vehicles = vehicleService.getVehicleList(keyword, pageNum, pageSize);
        return ResponseData.ok(vehicles);
    }

    @PostMapping("/add")
    public ResponseData<?> addVehicle(@RequestBody Vehicle vehicle) {
        boolean success = vehicleService.addVehicle(vehicle);
        return success ? ResponseData.ok() : ResponseData.fail(ResponseEnum.FAIL);
    }

    @DeleteMapping("/{id}")
    public ResponseData<?> deleteVehicle(@PathVariable("id") Long id) {
        boolean success = vehicleService.deleteVehicle(id);
        return success ? ResponseData.ok() : ResponseData.fail(ResponseEnum.FAIL);
    }

    @PostMapping("/update")
    public ResponseData<?> updateVehicle(@RequestBody Vehicle vehicle) {
        boolean success = vehicleService.updateVehicle(vehicle);
        return success ? ResponseData.ok() : ResponseData.fail(ResponseEnum.FAIL);
    }
}
